﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.Objects;

using WCFMINERP.DAL;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.SharpZipLib;
using WCFMINERP.MODEL;
using System.Xml;

namespace WCFMINERP.BLL.ShopBLL
{
    public class ShopBLL
    {
        /// <summary>
        /// 获取商户信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public ShopInfo GetShopInfo(string name, string pwd,string ShaCode)
        {
            ShopInfo shopInfo = new ShopInfo();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(s => s.FtpLoginName == name && s.FtpLoginPWD == pwd && s.SHACODE == ShaCode);
                if (shop != null)
                {
                    shopInfo.address = shop.address;
                    shopInfo.Balance = (decimal)shop.Balance;
                    shopInfo.FtpLoginName = shop.FtpLoginName;
                    shopInfo.FtpLoginPWD = shop.FtpLoginPWD;
                    shopInfo.mnemonicCode = shop.mnemonicCode;
                    shopInfo.ShopId = shop.ShopId;
                    shopInfo.shopKeeper = shop.shopKeeper;
                    shopInfo.shopName = shop.shopName;
                    shopInfo.Tel1 = shop.Tel1;
                    shopInfo.Tel2 = shop.Tel2;
                    shopInfo.ClientVersion = shop.ClientVersion;
                    shopInfo.ProductVersion = shop.ProductVersion;
                    shopInfo.SHACODE = shop.SHACODE;
                }
                else
                {
                    shopInfo = null;
                }
            }
            return shopInfo;
        }
        /// <summary>
        /// 获取商户信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public ShopInfo GetShopInfo(string name, string pwd)
        {
            ShopInfo shopInfo = new ShopInfo();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(s => s.FtpLoginName == name && s.FtpLoginPWD == pwd);
                if (shop != null)
                {
                    shopInfo.address = shop.address;
                    shopInfo.Balance = (decimal)shop.Balance;
                    shopInfo.FtpLoginName = shop.FtpLoginName;
                    shopInfo.FtpLoginPWD = shop.FtpLoginPWD;
                    shopInfo.mnemonicCode = shop.mnemonicCode;
                    shopInfo.ShopId = shop.ShopId;
                    shopInfo.shopKeeper = shop.shopKeeper;
                    shopInfo.shopName = shop.shopName;
                    shopInfo.Tel1 = shop.Tel1;
                    shopInfo.Tel2 = shop.Tel2;
                    shopInfo.ClientVersion = shop.ClientVersion;
                    shopInfo.ProductVersion = shop.ProductVersion;
                    shopInfo.SHACODE = shop.SHACODE;
                }
                else
                {
                    shopInfo = null;
                }
            }
            return shopInfo;
        }
        /// <summary>
        /// 获取客户端当前版本
        /// </summary>
        /// <param name="shopid"></param>
        /// <returns></returns>
        public VersionInfo GetCurrentVersion(int shopid)
        {
            VersionInfo version;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                 version.ClientVersion= db.Shop.FirstOrDefault(s=>s.ShopId ==shopid).ClientVersion;
                 version.ShopProductVersion = db.Shop.FirstOrDefault(s => s.ShopId == shopid).ProductVersion;
            }
            return version;
        }

        /// <summary>
        /// 检查是否需要更新
        /// </summary>
        /// <param name="ClientVersion"></param>
        /// <param name="shopid"></param>
        /// <returns></returns>
        public bool CheckUpdate(string ClientVersion,int shopid)
        {
            bool flag = false;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                double version = db.Shop.FirstOrDefault(s => s.ShopId == shopid).ClientVersion;
                if (version > Convert.ToDouble(ClientVersion))
                {
                    flag = true;
                }
            }
            return flag;
        }
        /// <summary>
        /// 获取要更新的文件
        /// </summary>
        /// <returns></returns>
        public TransferFileInfo GetFile()
        {
            TransferFileInfo tranFileInfo = new TransferFileInfo();
            string[] filePath = new string[2];
            
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                string dir = db.FileDirPath.FirstOrDefault(f => f.Des == "升级目录").FileDir;
                //filePath[0] = dir;
                //filePath[1] = dir + "\\update.zip";

                //ZipClass zip = new ZipClass();
                //zip.ZipFileMain(filePath);
                string[] fileArray = Directory.GetFiles(dir);
                var fileUpdatePath = fileArray.FirstOrDefault(p => p.EndsWith(".zip"));
                if (!string.IsNullOrEmpty(fileUpdatePath))
                {
                    tranFileInfo.Data = File.ReadAllBytes(fileUpdatePath);
                    tranFileInfo.Name = Path.GetFileName(fileUpdatePath);
                    //File.Delete(filePath[1]);
                }
                else
                {
                    throw new Exception("数据库没有设置升级目录！");
                }
            }
            return tranFileInfo;
        }
        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="shopid"></param>
        /// <returns></returns>
        public List<ShopProductInfo> GetProduct(int shopid, out List<PaperFaceInfo> paperFaceInfoList, out List<PaperTypeInfo> paperTypeInfoList,out List<CommonChargeItemInfo> commonChargeItemInfoList)
        {
            paperFaceInfoList = new List<PaperFaceInfo>();
            paperTypeInfoList = new List<PaperTypeInfo>();
            commonChargeItemInfoList = new List<CommonChargeItemInfo>();

            List<ShopProductInfo> shopProductInfoList = new List<ShopProductInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(s => s.ShopId == shopid);
                #region//获取纸面列表
                List<paperFace> listPaperFace =db.paperFace.ToList();
                foreach (var item in listPaperFace)
                {
                    if(item.Name !="任意")
                    {
                        PaperFaceInfo paperFaceInfo = new PaperFaceInfo();
                        paperFaceInfo.PaperFaceId =item.PaperFaceID;
                        paperFaceInfo.PaperFaceName =item.Name;
                        if (item.PaperFaceID == shop.PaperFaceId)
                        {
                            paperFaceInfo.DefaultPaperFace = true;
                        }
                        else
                        {
                            paperFaceInfo.DefaultPaperFace = false;
                        }
                        paperFaceInfoList.Add(paperFaceInfo);
                    }

                }
                #endregion

                #region//获取纸型列表
                if (!shop.Shop_Product_Price.IsLoaded)
                {
                    shop.Shop_Product_Price.Load();
                }
                if (!shop.Shop_Paper_Price.IsLoaded)
                {
                    shop.Shop_Paper_Price.Load();
                }

                foreach (var sPaperPrice in shop.Shop_Paper_Price)
	            {
                    if(!sPaperPrice.paperTypeReference.IsLoaded)
                    {
                        sPaperPrice.paperTypeReference.Load();
                    }
                    PaperTypeInfo papertypeInfo = new PaperTypeInfo();
                    papertypeInfo.areaPrice =sPaperPrice.UnitPrice;
                    papertypeInfo.paperTypeId =sPaperPrice.PaperTypeId;
                    papertypeInfo.paperTypeName =sPaperPrice.paperType.Name;
                    if (sPaperPrice.PaperTypeId == shop.PaperTypeId)
                    {
                        papertypeInfo.DefaultPaperType = true;
                    }
                    else
                    {
                        papertypeInfo.DefaultPaperType = false;
                    }
                    paperTypeInfoList.Add(papertypeInfo);
                }
                #endregion

                #region //获取公共收费项目列表
                if (!shop.Shop_CommonChargeItem_Price.IsLoaded)
                {
                    shop.Shop_CommonChargeItem_Price.Load();
                }
                foreach (var item in shop.Shop_CommonChargeItem_Price)
                {
                    if(!item.CommonChargeItemReference.IsLoaded)
                    {
                        item.CommonChargeItemReference.Load();
                    }
                    CommonChargeItemInfo comInfo = new CommonChargeItemInfo();
                    comInfo.CommonChargeItemId = item.CommonChargeItemId;
                    comInfo.CommonChargeItemName = item.CommonChargeItem.ItemName;
                    commonChargeItemInfoList.Add(comInfo);
                }
                #endregion
                //获取产品列表
                foreach (var item in shop.Shop_Product_Price)
                {

                    #region 加载数据
                    if (!item.Tb_ProductReference.IsLoaded)
                    {
                        item.Tb_ProductReference.Load();
                    }
                    if(!item.tb_CostTypeReference.IsLoaded)
                    {
                        item.tb_CostTypeReference.Load();
                    }
                    if (!item.Tb_Product.Tb_PorductCategoryReference.IsLoaded)
                    {
                        item.Tb_Product.Tb_PorductCategoryReference.Load();
                    }
                    if (!item.Tb_Product.CommonChargeItem.IsLoaded)
                    {
                        item.Tb_Product.CommonChargeItem.Load();
                    }
                    #endregion

                    if (item.Tb_Product.Status == 0)//已标志删除的产品
                    {
                        continue;
                    }
                    ShopProductInfo spInfo = new ShopProductInfo();
                    spInfo.areaPrice = 0;
                    spInfo.appendPrice = item.AppendPrice;
                    spInfo.costTypeName = item.tb_CostType.TypeName;
                    spInfo.coverHeight = item.Tb_Product.CoverHeight;
                    spInfo.coverWidth = item.Tb_Product.CoverWidth;
                    spInfo.height = item.Tb_Product.Height;
                    spInfo.mnemonicCode = item.Tb_Product.mnemonicCode;
                    spInfo.pageNum = item.Tb_Product.PageNum;
                    spInfo.productCategoryName = item.Tb_Product.Tb_PorductCategory.ProductCategoryName;
                    spInfo.productid = item.Tb_Product.ProuctId;
                    spInfo.unitPrice = (decimal)item.Tb_Product.UnitPrice;
                    spInfo.width = item.Tb_Product.Width;
                    spInfo.productName = item.Tb_Product.ProductName +"("+ spInfo.width + "X" + spInfo.height+")";
                    spInfo.UsedTimes = item.usedTimes == null ? 0 : (int)item.usedTimes;
                    string shopItemsString = "";
                    if (item.Tb_Product.CommonChargeItem.Count > 0)
                    {
                        foreach (CommonChargeItem cci in item.Tb_Product.CommonChargeItem)
                        {
                            Shop_CommonChargeItem_Price sccip = shop.Shop_CommonChargeItem_Price.FirstOrDefault(ccid => ccid.CommonChargeItemId == cci.CommonChargeItemId);
                            if (sccip != null)//商户中已经定义价格
                            {
                                shopItemsString += "," + cci.ItemName;
                            }
 
                        }
                        spInfo.CommonItemsString = shopItemsString.TrimStart(',');
                    }
                    else
                    {
                        spInfo.CommonItemsString = "";
                    }

                    shopProductInfoList.Add(spInfo);
                }         
            }
            return shopProductInfoList;
 
        }
        /// <summary>
        /// 注册新用户
        /// </summary>
        /// <param name="inUserID"></param>
        /// <param name="inPwd"></param>
        /// <param name="inShacode"></param>
        /// <returns></returns>
        public int RegisterNewUser(string inUserID, string inPwd, string inShacode)
        {
            int rtnVal = 0;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Shop checkShop = db.Shop.FirstOrDefault(sh => sh.FtpLoginName.Trim() == inUserID);
                    if (checkShop == null)
                    {
                        Shop currShop = db.Shop.FirstOrDefault(sh => sh.SHACODE == inShacode);
                        if (currShop == null)
                        {
                            rtnVal = -2;///注册码无无效
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(currShop.FtpLoginName))
                            {
                                rtnVal = 1;///该注册码已经使用过
                            }
                            else
                            {
                                currShop.FtpLoginName = inUserID;
                                currShop.FtpLoginPWD = inPwd;
                                rtnVal = 0;
                                db.SaveChanges();
                            }
                        }
                    }
                    else
                    {
                        rtnVal = -3;///用户名重复
                    }

                    
                       
                }
            }catch
            {
                rtnVal = -1;
            }
            return rtnVal;
        }
        /// <summary>
        /// 更改用户密码
        /// </summary>
        /// <param name="inUserID">登录名</param>
        /// <param name="inOldPwd">旧密码</param>
        /// <param name="inShacode">认证码</param>
        /// <param name="inNewPwd">新密码</param>
        /// <returns></returns>
        public int ChangeUserPwd(string inUserID, string inOldPwd, string inShacode, string inNewPwd)
        {
            int rtnVal = 0;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Shop shopCurr = db.Shop.FirstOrDefault(pt => pt.SHACODE == inShacode);
                    if (shopCurr == null)
                    {
                        rtnVal = -2;//认证码无效
                    }
                    else
                    {
                        if (shopCurr.FtpLoginName != inUserID)
                        {
                            rtnVal = -3;//登录名错误
                            return rtnVal;
                        }
                        if (shopCurr.FtpLoginPWD != inOldPwd)
                        {
                            rtnVal = -4;//旧密码错误
                            return rtnVal;
                        }
                        shopCurr.FtpLoginPWD = inNewPwd;
                        db.SaveChanges();
                        rtnVal = 0;//修改成功
                    }
                    db.SaveChanges();
                }

            }
            catch
            {
                rtnVal = -1;//数据错误
            }
            return rtnVal;
        }
       
    }
}
